using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace Watson.TemporaryFilesDeleter.Properties
{
    partial class Settings
    {
        #region Private Static Fields

        /// <summary>
        /// The current session settings.
        /// </summary>
        private static Settings currentSessionInstance = 
            ((Settings)(global::System.Configuration.ApplicationSettingsBase
                .Synchronized(new Settings())));

        #endregion

        #region Public Static Properties

        /// <summary>
        /// Gets the current session settings.
        /// </summary>
        public static Settings CurrentSession
        {
            get
            {
                return currentSessionInstance;
            }
        }

        #endregion

        #region Private Static Methods

        /// <summary>
        /// Handles the SettingsLoaded event for both the default and current
        /// session settings.
        /// </summary>
        /// <param name="sender">
        /// The sender of the event. Either <see cref="defaultInstance"/> or
        /// <see cref="currentSessionInstance"/>.
        /// </param>
        /// <param name="e">
        /// The event arguments.
        /// </param>
        private static void SettingsLoadedEventHandler(object sender,
            System.Configuration.SettingsLoadedEventArgs e)
        {
            Settings settings = sender as Settings;

            if (settings != null)
            {
                /*
                 * If the log file path is not specified, default to
                 * "%TEMP%\wtdf.log".
                 */
                if (String.IsNullOrWhiteSpace(settings.LogFilePath))
                {
                    settings.LogFilePath =
                        Path.Combine(Path.GetTempPath(), "wtfd.log");
                }

                /*
                 * Validate LogLevel is a valid enumeration member.
                 */
                if (!Enum.IsDefined(typeof(LogLevel), settings.LogLevel))
                {
                    settings.LogLevel = LogLevel.Error;
                }
            }
        }

        /// <summary>
        /// Handles the SettingsPropertyChanged event for both the default and 
        /// current session settings.
        /// </summary>
        /// <param name="sender">
        /// The sender of the event. Either <see cref="defaultInstance"/> or
        /// <see cref="currentSessionInstance"/>.
        /// </param>
        /// <param name="e">
        /// The event arguments.
        /// </param>
        private static void SettingsPropertyChangedEventHandler(object sender,
            System.ComponentModel.PropertyChangedEventArgs e)
        {
            Settings settings = sender as Settings;

            if (settings != null && e != null)
            {
                if (e.PropertyName == "LogFilePath")
                {
                    /*
                     * If the log file path is not specified, default to
                     * "%TEMP%\wtdf.log".
                     */
                    if (String.IsNullOrWhiteSpace(settings.LogFilePath))
                    {
                        settings.LogFilePath =
                            Path.Combine(Path.GetTempPath(), "wtfd.log");
                    }
                }
                

                if (e.PropertyName == "LogLevel")
                {
                    /*
                     * Validate LogLevel is a valid enumeration member.
                     */
                    if (!Enum.IsDefined(typeof(LogLevel), settings.LogLevel))
                    {
                        settings.LogLevel = LogLevel.Error;
                    }
                }
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Stores the current values of the application settings properties.
        /// </summary>
        public override void Save()
        {
            /*
             * Current session settings should not be persisted, only the
             * default settings should be persistable.
             */
            if (Object.ReferenceEquals(this, currentSessionInstance))
            {
                Debug.WriteLine("Prevented saving current session settings.");
                return;
            }

            base.Save();
        }

        #endregion

        #region Static Constructors

        /// <summary>
        /// Initializes static data for the Settings class.
        /// </summary>
        static Settings()
        {
            /*
             * Assign event handlers for the static default and current session
             * settings.
             */
            defaultInstance.SettingsLoaded           += 
                SettingsLoadedEventHandler;
            
            currentSessionInstance.SettingsLoaded    += 
                SettingsLoadedEventHandler;


            defaultInstance.PropertyChanged          +=
                SettingsPropertyChangedEventHandler;

            currentSessionInstance.PropertyChanged   +=
                SettingsPropertyChangedEventHandler;
        }

        #endregion
    }
}